package com.star.eastern.blee.utils

import android.annotation.SuppressLint
import android.os.AsyncTask
import android.util.Log
import okhttp3.MediaType
import java.io.ByteArrayOutputStream
import java.io.PrintStream
import java.nio.charset.Charset
import okhttp3.OkHttpClient
import okhttp3.Request
import okhttp3.RequestBody
import java.text.SimpleDateFormat
import java.util.*


val MEDIA_TYPE = MediaType.parse("text/plain; charset=utf-8")

class LogConsole {

    companion object {
        @JvmStatic
        var tagOfLog = "console"
        @JvmStatic
        var logList = arrayListOf<String>()
        @JvmStatic
        var viewLogState = false

        @JvmStatic
        fun writeBizLog(msg:String, e:Exception?){

            if(logList.size>100){
                for( i in 0 .. logList.size-100){
                    logList.removeAt(logList.lastIndex)
                }
            }


            val sdf = SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS ", Locale.ENGLISH)

            logList.add(0, sdf.format(Date())+msg)

            if(e is Exception){
                Log.e(tagOfLog, msg, e)
                val baos = ByteArrayOutputStream()
                val pos = PrintStream(baos)
                try{
                    pos.write(sdf.format(Date()).toByteArray(Charset.defaultCharset()))
                    pos.write(msg.toByteArray(Charset.defaultCharset()))
                    pos.write("\r\n".toByteArray(Charset.defaultCharset()))
                    e.printStackTrace(pos)
                    pos.write("\r\n".toByteArray(Charset.defaultCharset()))
                    pos.flush()
                    baos.flush()
                }catch (ex:java.lang.Exception){
                    Log.e(tagOfLog, ex.localizedMessage, ex)
                }
                val body = String(baos.toByteArray())
                remoteLog(body)

            }else{
                Log.i(tagOfLog, msg)

                val baos = ByteArrayOutputStream()
                val pos = PrintStream(baos)
                try{
                    pos.write(sdf.format(Date()).toByteArray(Charset.defaultCharset()))
                    pos.write(msg.toByteArray(Charset.defaultCharset()))
                    pos.write("\r\n".toByteArray(Charset.defaultCharset()))
                    pos.flush()
                    baos.flush()
                }catch (ex:java.lang.Exception){
                    Log.e("console", ex.localizedMessage, ex)
                }
                val body = String(baos.toByteArray())
                remoteLog(body)


            }
        }


        @JvmStatic
        fun writeBizLog(msg:String){
            writeBizLog(msg, null)
        }

        @SuppressLint("StaticFieldLeak")
        fun remoteLog(body:String){

            object:AsyncTask<Unit, Unit, Unit>(){
                override fun doInBackground(vararg params: Unit?) {
                    val client = OkHttpClient()
                    val rb = RequestBody.create(MEDIA_TYPE, body)
                    val request = Request.Builder()
                        .url("http://39.98.62.48/logs/save.php")
                        .post(rb)
                        .build()
                    try {
                        client.newCall(request).execute()
                    }catch (ex:Exception){
                        Log.e("console", ex.localizedMessage, ex)
                    }
                }

            }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR)

        }

        fun i(msg: String) {
            Log.i(tagOfLog, msg)
        }

        @Suppress("unused")
        fun w(msg: String) {
            Log.w(tagOfLog, msg)
        }
    }




}